/*
 * Copyright 2009 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.appengine.lib.client.login;

import nl.biz_e.appengine.lib.client.services.GoogleLoginService;
import nl.biz_e.appengine.lib.client.services.GoogleLoginServiceAsync;
import nl.biz_e.appengine.lib.shared.login.GoogleLoginInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Simple helper class for the Login service
 * 
 * @author Joost A. Bloemsma
 */
public class GoogleLogin {

    /**
     * Starts the call to the Google Authorisarion service, and on response
     * triggers the {@link GoogleLoginListener#onLoginEvent(GoogleLoginInfo)}
     * method.<br/>
     * Actually it's just a convinience method for
     * {@link #login(GoogleLoginListener, String)} without extension.
     * 
     * @param listener
     *            The requestor to be notified of the result
     */
    public static void login(final GoogleLoginListener listener) {
        login(listener, "");
    }

    /**
     * Starts the call to the Google Authorisarion service, and on response
     * triggers the {@link GoogleLoginListener#onLoginEvent(GoogleLoginInfo)}
     * method.
     * 
     * @param listener
     *            The requestor to be notified of the result
     * @param urlExt
     *            An optional extension to the root url.
     */
    public static void login(final GoogleLoginListener listener, String urlExt) {
        String uri = GWT.getHostPageBaseURL();
        if (urlExt.length() > 0) {
            if (!uri.endsWith("/"))
                uri += "/";
            if (urlExt.startsWith("/"))
                urlExt = urlExt.substring(1);
            uri += urlExt;
        }
        if (!GWT.isScript()) {
            if (uri.contains("?")) {
                uri += "&gwt.codesvr=127.0.0.1:9997";
            } else {
                uri += "?gwt.codesvr=127.0.0.1:9997";
            }
        }
        GoogleLoginServiceAsync loginService = GWT
                .create(GoogleLoginService.class);
        loginService.login(uri, new AsyncCallback<GoogleLoginInfo>() {
            public void onFailure(Throwable error) {
                GoogleLoginInfo info = new GoogleLoginInfo();
                info.setLoginError(true);
                listener.onLoginEvent(info);
            }

            public void onSuccess(GoogleLoginInfo result) {
                listener.onLoginEvent(result);
            }
        });
    }

    /**
     * Implement this interface to receive a notification of the login result
     * 
     * @author Joost A. Bloemsma
     */
    public interface GoogleLoginListener {
        /**
         * Is triggered when the service logged in
         * 
         * @param loginInfo
         */
        public void onLoginEvent(GoogleLoginInfo loginInfo);
    }
}
